<template>
  <div class="coupons-container">
    <!-- 顶部导航 -->
    <div class="nav-header">
      <div class="back-btn" @click="router.back()">
        <span class="back-icon">←</span>
      </div>
      <h1>我的优惠券</h1>
      <div class="get-coupon" @click="showCouponCenter = true">领券</div>
    </div>

    <!-- 标签页 -->
    <div class="tabs">
      <div
        v-for="(tab, index) in tabs"
        :key="index"
        class="tab-item"
        :class="{ active: currentTab === index }"
        @click="currentTab = index"
      >
        {{ tab.name }}
        <span class="count">({{ tab.count }})</span>
      </div>
    </div>

    <!-- 优惠券列表 -->
    <div class="coupons-list">
      <template v-if="filteredCoupons.length > 0">
        <div v-for="coupon in filteredCoupons" :key="coupon.id" class="coupon-card">
          <div class="coupon-left">
            <div class="amount">
              <span class="currency">¥</span>
              <span class="value">{{ coupon.amount }}</span>
            </div>
            <div class="condition">满{{ coupon.minSpend }}元可用</div>
          </div>
          <div class="coupon-right">
            <div class="coupon-info">
              <h3>{{ coupon.title }}</h3>
              <p class="valid-date">{{ coupon.validDate }}</p>
              <p class="description">{{ coupon.description }}</p>
            </div>
            <div class="coupon-status" :class="coupon.status">
              <span v-if="coupon.status === 'unused'">立即使用</span>
              <span v-else-if="coupon.status === 'used'">已使用</span>
              <span v-else>已过期</span>
            </div>
          </div>
        </div>
      </template>
      <div v-else class="empty-state">
        <div class="empty-icon">🎫</div>
        <p>暂无优惠券</p>
      </div>
    </div>

    <!-- 领券中心弹窗 -->
    <div class="modal" v-if="showCouponCenter" @click="showCouponCenter = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>领券中心</h3>
          <span class="close-btn" @click="showCouponCenter = false">×</span>
        </div>
        <div class="modal-body">
          <div v-for="coupon in availableCoupons" :key="coupon.id" class="available-coupon">
            <div class="coupon-info">
              <h4>{{ coupon.title }}</h4>
              <p>{{ coupon.description }}</p>
            </div>
            <button class="get-btn" @click="getCoupon(coupon)">领取</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const currentTab = ref(0)
const showCouponCenter = ref(false)

// 标签页数据
const tabs = [
  { name: '未使用', count: 2 },
  { name: '已使用', count: 1 },
  { name: '已过期', count: 1 }
]

// 优惠券数据
const coupons = [
  {
    id: 1,
    title: '新人专享券',
    amount: 50,
    minSpend: 100,
    validDate: '2024-02-01 - 2024-02-28',
    description: '仅限新用户使用',
    status: 'unused'
  },
  {
    id: 2,
    title: '周末特惠券',
    amount: 20,
    minSpend: 50,
    validDate: '2024-02-24 - 2024-02-25',
    description: '周末专享优惠',
    status: 'unused'
  },
  {
    id: 3,
    title: '生日礼券',
    amount: 100,
    minSpend: 200,
    validDate: '2024-01-01 - 2024-01-31',
    description: '生日当月专享',
    status: 'used'
  },
  {
    id: 4,
    title: '元旦优惠券',
    amount: 30,
    minSpend: 60,
    validDate: '2024-01-01 - 2024-01-03',
    description: '节日特惠',
    status: 'expired'
  }
]

// 可领取的优惠券
const availableCoupons = [
  {
    id: 5,
    title: '春节红包',
    description: '满200减100',
    amount: 100,
    minSpend: 200
  },
  {
    id: 6,
    title: '下午茶优惠',
    description: '满50减20',
    amount: 20,
    minSpend: 50
  }
]

// 根据当前标签筛选优惠券
const filteredCoupons = computed(() => {
  const statusMap = ['unused', 'used', 'expired']
  return coupons.filter(coupon => coupon.status === statusMap[currentTab.value])
})

// 领取优惠券
const getCoupon = (coupon: any) => {
  // 这里添加领取优惠券的逻辑
  alert(`已领取 ${coupon.title}`)
  showCouponCenter.value = false
}
</script>

<style scoped>
.coupons-container {
  padding-bottom: 24px;
  background-color: #f8f9fa;
  min-height: 100vh;
  max-width: 430px;
  margin: 0 auto;
}

.nav-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, #42b883 0%, #35495e 100%);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  color: white;
  height: 50px;
}

.back-icon {
  font-size: 24px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
  margin-right: 16px;
}

.back-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}


.nav-header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  flex: 1;
  text-align: center;
}

.get-coupon {
  color: #4f46e5;
  font-size: 14px;
  cursor: pointer;
}

.tabs {
    margin-top: 85px;
  display: flex;
  background: white;
  padding: 12px 20px;
  gap: 24px;
  border-bottom: 1px solid #eee;
}

.tab-item {
  position: relative;
  padding: 8px 0;
  color: #666;
  cursor: pointer;
  transition: color 0.3s;
}

.tab-item.active {
  color: #4f46e5;
  font-weight: 500;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: -12px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #4f46e5;
}

.count {
  font-size: 12px;
  color: #999;
}

.coupons-list {
  padding: 16px;
}

.coupon-card {
  display: flex;
  background: white;
  border-radius: 12px;
  margin-bottom: 16px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.coupon-left {
  width: 120px;
  padding: 20px;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.amount {
  display: flex;
  align-items: baseline;
}

.currency {
  font-size: 16px;
  margin-right: 2px;
}

.value {
  font-size: 32px;
  font-weight: bold;
}

.condition {
  font-size: 12px;
  margin-top: 8px;
  opacity: 0.9;
}

.coupon-right {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.coupon-info h3 {
  margin: 0 0 8px;
  font-size: 16px;
  color: #333;
}

.valid-date {
  font-size: 12px;
  color: #666;
  margin: 4px 0;
}

.description {
  font-size: 12px;
  color: #999;
  margin: 4px 0;
}

.coupon-status {
  text-align: right;
  font-size: 14px;
}

.coupon-status.unused {
  color: #4f46e5;
  cursor: pointer;
}

.coupon-status.used {
  color: #999;
}

.coupon-status.expired {
  color: #999;
}

.empty-state {
  text-align: center;
  padding: 40px 0;
  color: #999;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
}

.modal-content {
  background: white;
  border-radius: 16px;
  width: 90%;
  max-width: 400px;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-header {
  padding: 16px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
}

.close-btn {
  font-size: 24px;
  color: #999;
  cursor: pointer;
}

.modal-body {
  padding: 16px;
}

.available-coupon {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border: 1px solid #eee;
  border-radius: 8px;
  margin-bottom: 12px;
}

.available-coupon:last-child {
  margin-bottom: 0;
}

.get-btn {
  background: #4f46e5;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.get-btn:hover {
  background: #4338ca;
}
</style>